package cn.iocoder.yudao.module.erp.service.finance;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.category.PRCategorySaveVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.category.PRCategoryVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentCategoryDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinanceReceiptCategoryDO;
import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinanceReceiptCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Validated
public class ErpFinanceReceiptCategoryServiceImpl implements ErpFinanceReceiptCategoryService {
    @Resource
    private ErpFinanceReceiptCategoryMapper erpFinanceReceiptCategoryMapper;

    @Resource
    private CashFlowService cashFlowService;

    /**
     * 创建收入类别
     *
     * @param prCategorySaveVO 创建信息
     */
    @Override
    public void createFinanceReceiptCategory(PRCategorySaveVO prCategorySaveVO) {
       ErpFinanceReceiptCategoryDO category = BeanUtils.toBean(prCategorySaveVO, ErpFinanceReceiptCategoryDO.class);
       erpFinanceReceiptCategoryMapper.insert(category);
    }

    @Override
    public List<PRCategoryVO> getFinanceReceiptCategories() {
        // 1. 查询未删除的当前租户的付款类别
        List<ErpFinanceReceiptCategoryDO> categories = erpFinanceReceiptCategoryMapper.selectList();

        // 2. 转换为PRCategoryVO并设置现金流名称
        return categories.stream().map(category -> {
            PRCategoryVO vo = new PRCategoryVO();
            vo.setId(category.getId()); // 使用传入的类型参数
            vo.setName(category.getName());
            // 仅当有关联现金流时查询名称
            if (category.getCashFlow() != null) {
                vo.setCashFlow(cashFlowService.getCashFlowByID(category.getCashFlow()));
            }
            return vo;
        }).collect(Collectors.toList());
    }

}
